package com.example.myprojecth.controller;
import com.alibaba.fastjson.JSONObject;
import com.example.myprojecth.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.myprojecth.pojo.User;
import java.util.HashMap;
import java.util.Map;

@RestController
public class TheController {
    @Autowired
    private UserMapper userMapper;
    Map<String,User> map = new HashMap<String,User>();
    @PostMapping("/login")
    public String sendCount(@RequestBody JSONObject req) {
        String username = req.getString("name");
        String passwd = req.getString("passwd");
        System.out.println(username);
        System.out.println(passwd);
        String p = userMapper.selectPWD(username);
        if(p==null){
            return "login fail";
        }else if(p.equals(passwd)){
            return "login success";
        }
        return "login fail";

    }
    @PostMapping("/sign")
    public String signIn(@RequestBody JSONObject req){
        String username = req.getString("name");
        String passwd = req.getString("passwd");
        User user = userMapper.queryUserCount(username);
        if(user != null)  return "error";
        user = new User();
        user.setName(username);
        user.setPasswd(passwd);
        System.out.println(user.toString());
        userMapper.addUser(user);
        return "sign success";
    }


    @PostMapping("/getBNI")
    public float getBNI(@RequestBody JSONObject req){
        String username = req.getString("name");
        float BNI = userMapper.getBNI(username);
        return BNI;
    }

    @PostMapping("/updateBNI")
    public float updateBNI(@RequestBody JSONObject req)
    {
        String username = req.getString("name");
        float height = req.getFloat("height");
        float weight = req.getFloat("weight");
        float bni = weight / (height * height);
        userMapper.updateBNI(bni,username);
        return bni;

    }




}
